package com.utils;


import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JsonUtil {
    protected static Logger logger = LoggerFactory.getLogger(JsonUtil.class.getName());
    /*
* grid返回数据使用
* @data 数据源
* @total  总数量
* */
    public static Map<String, Object> JSONString(List<? extends Object> data, Long total) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("totalCount", total);
        map.put("rows", data);
        return map;
    }
    /**
     * 响应生成Grid格式的JSON数据集 <br>
     * 'totalCount':Grid的JSONReader的totalProperty属性必须设置为"totalCount" <br>
     * 'rows':Grid的JSONReader的root属性必须设置为"rows"
     */
    public static void JSONString(HttpServletResponse response, String jsonString, Long total)
            throws Exception {
        StringBuffer sb = new StringBuffer();
        sb.append("{\"totalCount\":" + total + ",\"rows\":");
        sb.append(jsonString);
        sb.append("}");

        response.setContentType("text/json; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(sb.toString());
    }



    /**
     * 响应生成Grid格式的JSON数据集 <br>
     * 'totalCount':Grid的JSONReader的totalProperty属性必须设置为"totalCount" <br>
     * 'rows':Grid的JSONReader的root属性必须设置为"rows"
     */
    public static void JSONString(HttpServletResponse response, List<? extends Object> data, Long total)
            throws Exception {

        String json= JSON.toJSONString(data);
        StringBuffer sb = new StringBuffer();
        sb.append("{\"totalCount\":" + total + ",\"rows\":");
        sb.append(json);
        sb.append("}");

        response.setContentType("text/json; charset=UTF-8");
        PrintWriter out = response.getWriter();

        //logger.debug(sb.toString());

        out.write(sb.toString());
    }



    /**
     * 响应生成Grid格式的JSON数据集 <br>
     * JSON对象的属性可以根据需要自定义设置，通过Map添加键值即可<br>
     * 如: Map<String,Object> map = new HashMap<String,Object>();<br>
     * map.put("totalCount",125);<br>
     * map.put("success",true);<br>
     * map.put("data",list);
     */
    public static void JSONString(HttpServletResponse response, Map<String, ? extends Object> data)
            throws Exception {
        String json=JSON.toJSONString(data);
        response.setContentType("text/json; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(json);
    }


    /**
     * 响应非Grid形式的JSON数据集的请求
     */
    public static void JSONString(HttpServletResponse response, List<? extends Object> data) throws Exception {
        String json=JSON.toJSONString(data);
        response.setContentType("text/json; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(json);
    }

    /**
     * 响应非Grid形式的JSON数据集的请求
     */
    public static void JSONString(HttpServletResponse response, JSONObject data) throws Exception {
        response.setContentType("text/json; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(data.toString());
    }
}
